using Godot;
using System;

public partial class Unit : CharacterBody3D, IPickable
{
	[Export] public float Speed = 1.0f;

	private MeshInstance3D pickRing;
	private Vector3 target;

	public override void _Ready()
	{
		pickRing = GetNode<MeshInstance3D>("PickRing");
	}

	public void OnPick()
	{
		pickRing.Visible = true;
		MoveCommand.Instance.onPick = (target) =>
		{
			this.target = target.position;
			GD.Print("Picked " + target.position);
		};
	}

	public void OnUnPick()
	{
		pickRing.Visible = false;
	}

	public override void _PhysicsProcess(double delta)
	{
		Vector3 difference = GlobalTransform.Origin.DirectionTo(target);
		difference.Y = 0;
		if ((target - GlobalTransform.Origin).Length() > 0.1f * Speed)
		{
			LookAt(difference + GlobalTransform.Origin);
			Velocity = Speed * difference;
		}
		else
		{
			Velocity = Vector3.Zero;
		}
		MoveAndSlide();
	}
}
